#include "rectboxitem.h"
#include "ui_rectboxitem.h"

#include <QDebug>
#include <QPropertyAnimation>

RectBoxItem::RectBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RectBoxItem)
{
    ui->setupUi(this);

    ui->musicImageBox->installEventFilter(this);



}

RectBoxItem::~RectBoxItem()
{
    delete ui;
}

void RectBoxItem::setRecText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RectBoxItem::setRecImage(const QString &imagePath)
{
    ui->recMusicImage->setPixmap(QPixmap(imagePath));
}

bool RectBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->musicImageBox)
    {
        if (event->type() == QEvent::Enter)
        {
           QPropertyAnimation* animal = new QPropertyAnimation(ui->musicImageBox,"geometry");
           animal->setDuration(150);
           animal->setStartValue(QRect(9,9,ui->musicImageBox->width(),ui->musicImageBox->height()));
           animal->setEndValue(QRect(9,0,ui->musicImageBox->width(),ui->musicImageBox->height()));
           animal->start();

           connect(animal,&QPropertyAnimation::finished,this,[=](){
               delete animal;
           });
        }
        else if (QEvent::Leave == event->type())
        {
            QPropertyAnimation* animal = new QPropertyAnimation(ui->musicImageBox,"geometry");
            animal->setDuration(150);
            animal->setStartValue(QRect(9,0,ui->musicImageBox->width(),ui->musicImageBox->height()));
            animal->setEndValue(QRect(9,9,ui->musicImageBox->width(),ui->musicImageBox->height()));
            animal->start();

            connect(animal,&QPropertyAnimation::finished,this,[=](){
                delete animal;
            });

        }

        return true;
    }

    return this->QObject::eventFilter(watched,event);
}
